package com.zt.bus.assistant.controller;

import com.zt.bus.assistant.service.HolidayService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.util.DateUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/holiday")
public class HolidayController {

	@Autowired
	private HolidayService holidayService;

	@RequestMapping(value = "/query")
	@ResponseBody
	public JSONObject query(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
		return Response.response(holidayService.getTable(reqBean));
	}
	@RequestMapping(value = "/save")
	@ResponseBody
	public JSONObject save(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
		int year = reqBean.getInfoInt("year");
		String[] dates = reqBean.getInfo("dates").split(",");
		List<Date> list = new ArrayList<>();
		for (String date : dates) {
			list.add(DateUtil.parse(date));
		}
		holidayService.save(year, list);
		return Response.responseSuccess();
	}
}